window.onload = function() {	
  document.getElementById("loginButton").onclick = ajaxLogin;
}

function ajaxLogin() {
  var obj;
  var url = "login.jsp";
  var params = "uname=" + document.getElementsByName("uname")[0].value + "&pass=" + document.getElementsByName("pass")[0].value;
  if(window.XMLHttpRequest) {	// Major browsers
    obj = new XMLHttpRequest();
    obj.open("POST",url,true); 
    obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    obj.setRequestHeader("Content-length", params.length);
    obj.setRequestHeader("Connection", "close");
    obj.onreadystatechange = function() {
      if(obj.readyState == 4) {
        if(obj.status == 200) {
          if(obj.responseText == "no") {
            alert("Wrong username/password");
          } else {
            window.open("profile.jsp?id="+obj.responseText,"_self",false);
          }
        } else {
          alert("Problem in returned data" + " Error status=" + obj.status);
        }
      }
    }
    obj.send(params);
  } else if(window.ActiveXObject) { // IE 5 & 6
    obj = new ActiveXObject("Microsoft.XMLHTTP");
    if(obj) {
      obj.onreadystatechange = function() {			
        if(obj.readyState == 4) {
          if(obj.status == 200) {
            // nothing to do here...
          } else {
            alert("Problem in returned data");
          }
        }
      }
      obj.open("POST",url,true);
      obj.send();
    }
  } else {
    alert("Your browser doesn't support AJAX");
  }
}
